﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using SiteServer.Plugin;
using Thingpower.SS.EduOnline.Model;

namespace Thingpower.SS.EduOnline.Provider
{
    public class CoursewareStudyStatisticDAO : BaseDAO
    {
        public const string TableName = "view_CoursewareStudyStatistic";

        #region 获取课程参与人数和浏览量
        public static List<CoursewareStudyStatisticInfo> GetAllModelsList()
        {
            var list = new List<CoursewareStudyStatisticInfo>();

            string sqlString =
                $"SELECT CoursewareId, CoursewareTitle, UsersCount, TimesCount, VideoUrl, FileUrl, ImageUrl FROM {TableName} ORDER BY {nameof(CoursewareStudyStatisticInfo.UsersCount)} DESC, {nameof(CoursewareStudyStatisticInfo.TimesCount)} DESC";

            using (var rdr = DataApi.ExecuteReader(ConnectionString, sqlString))
            {
                while (rdr.Read())
                {
                    list.Add(GetModel(rdr));
                }
                rdr.Close();
            }

            return list;
        }
        #endregion

        #region 获取热门课程
        public static List<CoursewareStudyStatisticInfo> GetTopModelsList(int topNum)
        {
            var list = new List<CoursewareStudyStatisticInfo>();

            string sqlString =
                $"SELECT TOP {topNum} CoursewareId, CoursewareTitle, UsersCount, TimesCount, VideoUrl, FileUrl, ImageUrl FROM {TableName} ORDER BY {nameof(CoursewareStudyStatisticInfo.UsersCount)} DESC, {nameof(CoursewareStudyStatisticInfo.TimesCount)} DESC";

            using (var rdr = DataApi.ExecuteReader(ConnectionString, sqlString))
            {
                while (rdr.Read())
                {
                    list.Add(GetModel(rdr));
                }
                rdr.Close();
            }

            return list;
        }
        #endregion

        #region 获取指定课程参与人数和浏览量
        private static CoursewareStudyStatisticInfo GetModel(IDataRecord rdr)
        {
            if (rdr == null) return null;

            var _model = new CoursewareStudyStatisticInfo();

            var i = 0;
            _model.CoursewareId = rdr.IsDBNull(i) ? 0 : rdr.GetInt32(i);
            i++;
            _model.CoursewareTitle = rdr.IsDBNull(i) ? string.Empty : rdr.GetString(i);
            i++;
            _model.UsersCount = rdr.IsDBNull(i) ? 0 : rdr.GetInt32(i);
            i++;
            _model.TimesCount = rdr.IsDBNull(i) ? 0 : rdr.GetInt32(i);
            i++;
            _model.VideoUrl = rdr.IsDBNull(i) ? string.Empty : rdr.GetString(i);
            i++;
            _model.FileUrl = rdr.IsDBNull(i) ? string.Empty : rdr.GetString(i);
            i++;
            _model.ImageUrl = rdr.IsDBNull(i) ? string.Empty : rdr.GetString(i).Substring(1);

            if (!string.IsNullOrEmpty(_model.VideoUrl))
            {
                _model.Category = CoursewareCategory.Video;
            }

            if (!string.IsNullOrEmpty(_model.FileUrl))
            {
                _model.Category = CoursewareCategory.PDF;
            }

            return _model;
        }
        #endregion
    }
}